/**
 * @param {string} num
 * @param {number[]} change
 * @return {string}
 */
 var maximumNumber = function(num, change) {
    const arr = num.split('')
    const n = arr.length
    let flag = true, pre = false
    for(let i = 0; i<n; i++){
        if(change[arr[i]] > arr[i] && flag){
            arr[i] = change[arr[i]]
            pre = true
        }else if(change[arr[i]] === arr[i] && pre){
            pre = true
        }else if(change[arr[i]] < arr[i] && pre){
            flag = false
        }
    }
    return arr.join('')
};